package com.auemall.controller;

import javax.servlet.http.HttpServletRequest;

import com.auemall.po.Deliverinfo;
import com.auemall.po.Order;
import com.auemall.po.ex.ProductEx;
import com.auemall.utils.SecurityUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.auemall.po.Buyer;
import com.auemall.service.BuyerService;
import com.auemall.service.DeliverinfoService;
import com.auemall.service.FavoriteService;
import com.auemall.service.OrderService;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

@Controller
public class MemberController {
	
	@Autowired
	private BuyerService buyService;
	
	@Autowired
	private OrderService orderService;
	
	@Autowired
	private DeliverinfoService deliverinfoService;
	
	@Autowired
	private FavoriteService favoriteService;
	
	

	@RequestMapping("/member/mycenter")
	public String showMember(HttpServletRequest request) {
		
		return "member/userDefault";
	}
	
	//订单管理
	@RequestMapping("/member/userOrders")
	public String orderManager(HttpServletRequest request, Model model, @RequestParam Map<String, Object> paramMap) {
		/*//接收参数
		//从session中获取登录id,需要处理--------
		Integer buyid = null;
		Integer pageNum = Integer.valueOf(String.valueOf(paramMap.get("pageNum")));
		pageNum = pageNum == null ? 1 : pageNum;
		//默认显示10条
		int pageSize = 10;
		PageHelper.startPage(pageNum, pageSize);
		Page<Order> page = (Page<Order>) this.orderService.findAllOrder(paramMap);
		//当前页结果集
		List<Order> list = page.getResult();
		//共多少页
		int pages = page.getPages();
		//共多少条记录
		long total = page.getTotal();
		model.addAttribute("pageNum", page.getPageNum());
		model.addAttribute("pages", pages);
		model.addAttribute("total", total);
		model.addAttribute("list", list);
*/
		return "member/userOrders";
	}
	
	
	//订单查看
	@RequestMapping("/member/orderDetail")
	public String orderDetail(HttpServletRequest request) {
		
		return "member/orderDetails";
	}
	
	
	//退款申请单
	@RequestMapping("/member/userRefundApply")
	public String userRefundApply(HttpServletRequest request) {
		
		return "member/userRefundApply";
	}
	
	//退货申请单
	@RequestMapping("/member/userReturnsApply")
	public String userReturnsApply(HttpServletRequest request) {
		
		return "member/userReturnsApply";
	}
	
	
	//换货申请单
	@RequestMapping("/member/userReplaceApply")
	public String userReplaceApply(HttpServletRequest request) {
		
		return "member/userReplaceApply";
	}
	
	//我的积分
	@RequestMapping("/member/userPoints")
	public String userPoints(HttpServletRequest request) {
		
		return "member/userPoints";
	}
	
	//我的优惠券
	@RequestMapping("/member/myCoupons")
	public String myCoupons(HttpServletRequest request) {
		
		return "member/myCoupons";
	}
	
	
	//收藏夹
	@RequestMapping("/member/favorites")
	public String favorites(HttpServletRequest request, Model model, Integer pageNum) {
		//从session中获取登录id,需要处理--------
		Integer buyid = null;
		//接收分页参数
		pageNum = pageNum == null ? 1 : pageNum;
		//默认显示10条
		int pageSize = 10;
		PageHelper.startPage(pageNum, pageSize);
		Page<ProductEx> page = (Page<ProductEx>) this.favoriteService.findAllFavorite(buyid);
		//当前页结果集
		List<ProductEx> list = page.getResult();
		//共多少页
		int pages = page.getPages();
		//共多少条记录
		long total = page.getTotal();
		model.addAttribute("pageNum", page.getPageNum());
		model.addAttribute("pages", pages);
		model.addAttribute("total", total);
		model.addAttribute("list", list);
		return "member/favorites";
	}
	
	//咨询回复
	@RequestMapping("/member/userConsultations")
	public String userConsultations(HttpServletRequest request) {
		
		return "member/userConsultations";
	}
	
	//我参与的评论
	@RequestMapping("/member/userProductReviews")
	public String userProductReviews(HttpServletRequest request) {
		
		return "member/userProductReviews";
	}
	
	
	//商品批量购买
	@RequestMapping("/member/userBatchBuy")
	public String userBatchBuy(HttpServletRequest request) {
		
		return "member/userBatchBuy";
	}
	
	//预付款账户
	@RequestMapping("/member/myAccountSummary")
	public String myAccountSummary(HttpServletRequest request) {
		
		return "member/myAccountSummary";
	}
	
	//预付款充值
	@RequestMapping("/member/rechargeRequest")
	public String rechargeRequest(HttpServletRequest request) {
		
		return "member/rechargeRequest";
	}
	
	
    //账户安全
	@RequestMapping("/member/myAccount")
	public String myAccount(HttpServletRequest request) {
		
		return "member/myAccount";
	}

	/**
	 * 获取个人信息
	 *
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/member/userProfile", method = RequestMethod.GET)
	public String userProfile(HttpServletRequest request, Model model) {
		//从session中获取登录id,需要处理--------
		Integer id = null;
		Buyer buyer = null;
		if (null != id) {
			buyer = this.buyService.selectByPrimaryKey(id);
		}
		model.addAttribute("buyer", buyer);
		return "member/userProfile";
	}

	/**
	 * 保存个人信息
	 *
	 * @param request
	 * @param model
	 * @param buyer
	 * @return
	 */
	@RequestMapping(value = "/member/userProfile", method = RequestMethod.POST)
	public String saveUserProfile(HttpServletRequest request, Model model, Buyer buyer) {
		//日期类型特殊处理
		String birthday = request.getParameter("birthdayName");
		if(null != buyer && null != buyer.getId()) {
			if(null != birthday && !"".equals(birthday.trim())){
				buyer.setBirthday(DateUtils.parseDate(birthday));
			}
			int result = this.buyService.updateByPrimaryKeySelective(buyer);
		}
		model.addAttribute("buyer", buyer);
		return "member/userProfile";
	}
	
	//修改密码
	@RequestMapping(value = "/member/updatePassword", method = RequestMethod.GET)
	public String updatePassword(HttpServletRequest request, Model model) {
		//从session中获取登录id,需要处理--------
		Integer id = null;
		model.addAttribute("id", id);
		return "member/updatePassword";
	}

	//修改密码
	@RequestMapping(value = "/member/updatePassword", method = RequestMethod.POST)
	public String updatePassword(HttpServletRequest request, Model model, String oldPwd, String newPwd) {
		//从session获取登录id,需要处理--------
		Integer id = null;
		String msg = "密码修改失败";
		if(null != id) {
			if (null != oldPwd && null != newPwd) {
				Buyer buyer = this.buyService.selectByPrimaryKey(id);
				if(null != buyer){
					//加密后的密码
					String rightPwd = buyer.getPassword();
					if(null != rightPwd) {
						if (rightPwd.equals(SecurityUtils.encodePassword(oldPwd))) {
							int result = this.buyService.updateByPrimaryKeySelective(buyer);
							if(result > 0){
								msg = "密码修改成功";
							}
						}
					}
				}
			}
		}
		model.addAttribute("msg", msg);
		return "member/updatePassword";
	}

	//收货地址-获取收货地址列表
	@RequestMapping("/member/userShippingAddresses")
	public String userShippingAddresses(HttpServletRequest request, Model model) {
		//从session中获取登录id,需要处理--------
		Integer buyid = null;
		List<Deliverinfo> deliverinfoList =  deliverinfoService.findAll(buyid);
		request.setAttribute("list",deliverinfoList);
		return "member/userShippingAddresses";
	}

	//收货地址-获取收货地址列表
	@RequestMapping(value = "/member/userShippingAddresses/save", method = RequestMethod.POST)
	public String saveUserShippingAddresses(HttpServletRequest request, Model model, Deliverinfo info) {
		int result =  deliverinfoService.insertSelective(info);
		if(result > 0){
			model.addAttribute("msg", "添加成功");
		}else {
			model.addAttribute("msg", "添加失败");
		}
		return "member/userShippingAddresses";
	}

	//收货地址-删除
	@RequestMapping("/member/userShippingAddresses/del")
	public String delUserShippingAddresses(HttpServletRequest request, Model model, Integer id) {
		int result =  deliverinfoService.deleteByPrimaryKey(id);
		if(result > 0){
			model.addAttribute("msg", "删除成功");
		}else {
			model.addAttribute("msg", "删除失败");
		}
		return "member/userShippingAddresses";
	}

	//收件箱
	@RequestMapping("/member/userReceivedMessages")
	public String userReceivedMessages(HttpServletRequest request) {
		
		return "member/userReceivedMessages";
	}
	
	//发件箱
	@RequestMapping("/member/userSendedMessages")
	public String userSendedMessages(HttpServletRequest request) {
		
		return "member/userSendedMessages";
	}
	
	//给管理员发消息
	@RequestMapping("/member/userSendMessage")
	public String userSendMessage(HttpServletRequest request) {
		
		return "member/userSendMessage";
	}
	
	//退出
	@RequestMapping("/member/logout")
	public String logout (HttpServletRequest request) {
		
		request.getSession().removeAttribute("username");
		
		return "redirect:/front/index";
	}
	
}
